package com.bruceliu.controller;

import com.bruceliu.bean.FoodType;
import com.bruceliu.service.FoodTypeService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @BelongsProject: RestaurantSystem
 * @BelongsPackage: com.bruceliu.controller
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-02-29 12:55
 * @Description: TODO
 */
@Controller
@Scope("prototype")
public class FoodTypeController {

    @Resource
    FoodTypeService foodTypeService;

    @RequestMapping("/foodType_queryList")
    public String queryList(FoodType foodType, Model model){
        List<FoodType> foodTypes = foodTypeService.getFoodTypes(foodType);
        System.out.println("查询到的页面数据是:"+foodTypes);
        model.addAttribute("foodTypes",foodTypes);
        //要回显的数据对象
        model.addAttribute("foodType",foodType);
        return "foodTypeList";
    }

    @RequestMapping("/foodType_add")
    public void addFoodType(FoodType foodType, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        int count = foodTypeService.addFoodType(foodType);
        if (count > 0) {
            response.getWriter().write("<script>alert('菜系新增成功!');location.href='/foodType_queryList';</script>");
        }else{
            response.getWriter().write("<script>alert('菜系新增失败!');location.href='/page_saveFoodType';</script>");
        }
    }

    @RequestMapping("/foodType_toupdate/{id}")
    public String toupdate(@PathVariable("id") int id,Model model){
        FoodType foodType = foodTypeService.getById(id);
        model.addAttribute("foodType",foodType);
        return "updateFoodType";
    }

    @RequestMapping("/foodType_update")
    public void update(FoodType foodType, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        int count = foodTypeService.updateFoodType(foodType);
        if (count > 0) {
            response.getWriter().write("<script>alert('菜系更新成功!');location.href='/foodType_queryList';</script>");
        }else{
            response.getWriter().write("<script>alert('菜系更新失败!');location.href='/foodType_toupdate/"+foodType.getId()+"';</script>");
        }
    }

    @RequestMapping("/foodType_delete/{id}")
    public void delete(@PathVariable("id") int id, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        int count = foodTypeService.deleteFoodType(id);
        if (count > 0) {
            response.getWriter().write("<script>alert('菜系删除成功!');location.href='/foodType_queryList';</script>");
        }else{
            response.getWriter().write("<script>alert('菜系删除失败!');location.href='/foodType_queryList';</script>");
        }
    }
}
